package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.atguigu.jxc.dao.PurchaseListDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.vo.PurchaseListCountVo;
import com.atguigu.jxc.service.PurchaseListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 杨林
 * @create 2023-01-29 20:55 星期日
 * description:
 */
@Service
public class PurchaseListServiceImpl implements PurchaseListService {

    @Autowired
    PurchaseListDao purchaseListDao;

    @Autowired
    PurchaseListGoodsDao purchaseListGoodsDao;

    /**
     * 将purchaseList存入数据库
     * @param purchaseList
     */
    @Override
    public void save(PurchaseList purchaseList) {
        purchaseListDao.save(purchaseList);
    }

    /**
     * 进货单删除
     * @param purchaseListId
     * @return
     */
    @Override
    public ServiceVO delete(Integer purchaseListId) {

        //判断当前进货单下是否有商品   如果有商品，则该进货单不能删除
        List<PurchaseListGoods> list = new ArrayList<>();
        list = purchaseListGoodsDao.ifHaspurchaseListGoods(purchaseListId);
        if (list.size() > 0){
            //当前进货单下有商品  不能删除
            return new ServiceVO(ErrorCode.STORED_ERROR_CODE,ErrorCode.STORED_ERROR_MESS);
        }

        purchaseListDao.delete(purchaseListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /**
     * 支付结算（修改进货单付款状态）
     * @param purchaseListId
     * @return
     */
    @Override
    public ServiceVO updateState(Integer purchaseListId) {

        purchaseListDao.updateState(purchaseListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /**
     * 进货统计（可根据 商品类别、商品编码或名称 条件查询）
     * @param sTime
     * @param eTime
     * @param goodsTypeId
     * @param codeOrName
     * @return
     */
    @Override
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {

        List<PurchaseListCountVo> purchaseListCountVoList =
                purchaseListDao.count(sTime,eTime,goodsTypeId,codeOrName);

        String s = JSON.toJSONString(purchaseListCountVoList);
        return s;
    }
}
